package com.waidi.clothespad.common;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class QrCodeInfo {

    public String name;
    public String num;

    public String factory;
    public String workshop;
    public String line;
    public String workstation;

    public String qrcodefile;

    public String location;

    public  QrCodeInfo()
    {
        name="#";
        num="#";
        factory="#";
        workshop="#";
        line="#";
        workstation="#";
        location=new String();
        qrcodefile= Config.Clothesath+"/qrcode.txt";
        QrCodeInfoRead();
    }

    public void QrCodeInfoWrite()
    {
        try
        {
            JSONObject qrcode = new JSONObject();
            qrcode.put("name", name);
            qrcode.put("num", num);
            //
            qrcode.put("factory", factory);
            qrcode.put("workshop", workshop);
            qrcode.put("line", line);
            qrcode.put("workstation", workstation);
            qrcode.put("location", location);

            String jsonstr = qrcode.toString();
            OutputStream os = new FileOutputStream(qrcodefile);
            byte [] bytes = jsonstr.getBytes("UTF-8");
            os.write(bytes,0,bytes.length);
            //关闭流
            os.close();
            os.flush();
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
        catch (IOException i)
        {
            i.printStackTrace();
        }

    }

    public void QrCodeInfoRead()
    {
        File file = new File(qrcodefile);
        try {
            FileInputStream in = new FileInputStream(file);
            int len = 0;
            byte[] buf = new byte[1024];
            String  strbuf;
            String  strjson="";
            while((len=in.read(buf))!=-1){
                strbuf = new String(buf,0,len);
                strjson = strjson + strbuf;
            }
            in.close();
            if(strjson==null)
                return;
            JSONObject jsonObj = new JSONObject(strjson);
            if(jsonObj==null)
                return;
            name = jsonObj.optString("name");
            num = jsonObj.optString("num");
            factory = jsonObj.optString("factory");
            workshop = jsonObj.optString("workshop");
            workstation = jsonObj.optString("workstation");
            line = jsonObj.optString("line");
            location = jsonObj.optString("location");

        }
        catch (JSONException je)
        {
            je.printStackTrace();
        }catch (IOException e)
        {
            e.printStackTrace();
        }


    }


}
